package day12;

import java.util.Scanner;

// 学生类
class Student {
    private String name;
    private int age;
    private String gender;

    // 无参构造器
    public Student() {
    }

    // 有参构造器
    public Student(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    // getter和setter方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

// 学生管理系统类
public class StudentManagement {
    // 学生对象数组
    private Student[] stus;
    // 实际学生个数
    private int counter;
    // 键盘录入对象
    private Scanner in;

    // 无参构造器：默认容量100
    public StudentManagement() {
        stus = new Student[100];
        in = new Scanner(System.in);
    }

    // 有参构造器：size表示初始容量
    public StudentManagement(int size) {
        stus = new Student[size];
        in = new Scanner(System.in);
    }

    // 输出操作提示
    public void prompt() {
        System.out.println("********************");
        System.out.println("    学生管理系统");
        System.out.println("********************");
        System.out.println("(1) 添加学生");
        System.out.println("(2) 删除学生");
        System.out.println("(3) 更新信息");
        System.out.println("(4) 查询学生");
        System.out.println("(0) 退出系统");
        System.out.println("********************");
        System.out.print("请选择你的操作: ");
    }

    // 根据用户录入的学生名称，找到学生在数组中的索引返回
    private int findStudent() {
        int index = -1;
        System.out.print("请输入姓名: ");
        String name = in.next();
        
        for (int i = 0; i < counter; i++) {
            if (stus[i] != null && stus[i].getName().equals(name)) {
                index = i;
                break;
            }
        }
        return index;
    }

    // 添加学生
    public void addStudent() {
        System.out.print("请输入姓名: ");
        String name = in.next();
        System.out.print("请输入年龄: ");
        int age = in.nextInt();
        System.out.print("请输入性别: ");
        String gender = in.next();
        
        if (counter < stus.length) {
            stus[counter] = new Student(name, age, gender);
            counter++;
            System.out.println("添加成功");
        } else {
            // 创建一个新数组，容量为原数组的2倍
            Student[] newStus = new Student[stus.length * 2];
            // 将原数组内容复制到新数组
            for (int i = 0; i < stus.length; i++) {
                newStus[i] = stus[i];
            }
            // 将新数组赋值给原数组引用
            stus = newStus;
            // 添加新学生到新数组
            stus[counter] = new Student(name, age, gender);
            counter++;
            System.out.println("添加成功（数组已扩容）");
        }
    }

    // 删除学生
    public void deleteStudent() {
        int index = findStudent();
        if (index != -1) {
            for (int i = index; i < counter - 1; i++) {
                stus[i] = stus[i + 1];
            }
            stus[counter - 1] = null;
            counter--;
            System.out.println("删除成功");
        } else {
            System.out.println("找不到目标学生");
        }
    }

    // 更新学生信息
    public void updateStudent() {
        int index = findStudent();
        if (index != -1) {
            System.out.print("请输入新的年龄: ");
            int age = in.nextInt();
            System.out.print("请输入新的性别: ");
            String gender = in.next();
            
            stus[index].setAge(age);
            stus[index].setGender(gender);
            System.out.println("更新成功");
        } else {
            System.out.println("找不到目标学生");
        }
    }

    // 查询学生信息
    public void selectStudent() {
        int index = findStudent();
        if (index != -1) {
            Student s = stus[index];
            System.out.println("Name: " + s.getName());
            System.out.println("Age: " + s.getAge());
            System.out.println("Gender: " + s.getGender());
        } else {
            System.out.println("找不到目标学生");
        }
    }

    public static void main(String[] args) {
        // 实例化对象
        StudentManagement sm = new StudentManagement();
        // 用户操作标志
        int option = -1;
        
        Loop: while (true) {
            // 打印提示信息
            sm.prompt();
            // 获取用户操作
            option = sm.in.nextInt();
            
            if (option < 0 || option > 4) {
                System.out.println("错误操作，请重试");
                continue;
            }
            
            switch (option) {
                case 1:
                    sm.addStudent();
                    break;
                case 2:
                    sm.deleteStudent();
                    break;
                case 3:
                    sm.updateStudent();
                    break;
                case 4:
                    sm.selectStudent();
                    break;
                case 0:
                    break Loop;
            }
        }
        
        System.out.println("游戏结束，再见");
    }
}
